(function(){
    "use strict";
    app.controller('developCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE','$http',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE,$http) {
            $rootScope.moduleTitle = '应用管理 > 应用部署';//定义当前页
            $rootScope.link = '/statics/css/common.css';//引入页面样式
            $scope.isBack = false;
            var flag = 1;
            if(flag==null){
                $scope.isBack = false;
            }else{
                $scope.isBack = true;
            }
            $scope.goBack = function(){
                if(flag==1){
                    $state.go('app.containerManger.net');
                }
            };
            $scope.param = {
                rows: 10
            };
            $scope.isbatchDelete = true;
            //获取主机列表

            $scope.showimg = function($event){
                $($event.target).parent().next().show();
                $($event.target).hide();
                $($event.target).next().show();
            }
            $scope.hideimg = function($event){
                $($event.target).parent().next().hide();
                $($event.target).hide();
                $($event.target).prev().show();
            }

            //跳转详情页
            $scope.detail = function(item){
                $state.go('app.application.developDetail',{id:item.applicationCoreId,appName:item.applicationCoreName,appCoreRename:item.applicationCoreRename,flag:0});
            };

            //跳转详情页
            $scope.add = function(){
                $state.go('app.application.createDeploy');
            };

            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows,
                        "applicationCore.applicationCoreUsername" : "default",
                        "applicationCore.applicationCoreName":$scope.searchByName
                    };
                
                httpLoad.loadData({
                	url:'/application/getApplicationListByUsername.do',
                    data: params,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                        	$scope.appList = data.rows;
                            $scope.totalCount = data.records;
                            $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
                
//                $http({  
//                	url:'/application/getApplicationListByUsername.do',
//                    method: 'POST',
//                    dataType:"json",
//                    contentType: "application/json",
////                  data:params
//                   data:{page:"1",rows:"10","applicationCore.applicationCoreUsername" : "default"}  
//                }).success(function(data){
//                        if(data.success&&data.rows&&data.rows.length!=0){
//                            $scope.appList = data.rows;
//                            $scope.totalCount = data.total;
//                            $scope.isImageData = false;
//                        }else{
//                            $scope.isImageData = true;
//                        }
//                })
                
            };
            $scope.getData(1);

            //启用
            $scope.start = function(){  //打开模态
                var isc = "";
                $("input[name=appInfo]:checked").each(function(){
                        isc += $(this).val() + ",";
                });
                if(isc.length > 0) {
                    isc = isc.substring(0, isc.length - 1);
                }else{
                    $scope.pop("请先选择应用",'error');
                    return false;
                }
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/application/develop/start.html',
                    controller : 'startModalCtrl',
                    resolve : {
                        id: function() {
                            return  isc;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
            //停止
            $scope.stop = function(){  //打开模态
                var isc = "";
                $("input[name=appInfo]:checked").each(function(){
                    isc += $(this).val() + ",";
                });
                if(isc.length > 0) {
                    isc = isc.substring(0, isc.length - 1);
                }else{
                    $scope.pop("请先选择应用",'error');
                    return false;
                }
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/application/develop/stop.html',
                    controller : 'stopModalCtrl',
                    resolve : {
                        id: function() {
                            return  isc;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
            //删除
            var isc = "";
            $scope.choseId = function(id){
                isc = id;
            }
            $scope.delete = function(){  //打开模态
                if(isc == "") {
                    $scope.pop("请先选择应用",'error');
                    return false;
                }
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/application/develop/delete.html',
                    controller : 'deleteModalCtrl',
                    resolve : {
                        id: function() {
                            return  isc;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
            //重新构建
            $scope.reBuild = function(item){
                $state.go('app.application.appCreateIn',{id:item.id,flag:1});
            };
        }
    ]);
    //启用ctrl
    angular.module('app').controller('startModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/host/delete',
                    method:'POST',
                    data:{id: id},
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
    //删除ctrl
    angular.module('app').controller('stopModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/host/delete',
                    method:'POST',
                    data:{id: id},
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
    //删除ctrl
    angular.module('app').controller('deleteModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/application/deleteApplication.do',
                    data:{"applicationCore.applicationCoreId": id},
                    success:function(data){
                        if(data.success){
                            // $scope.pop(data.message);
                            httpLoad.loadData({
                                url:'/pipeline/pipeline/deleteProject',
                                data:{projectId: id},
                                success:function(data){
                                    if(data.success){
                                        $scope.pop(data.message);
                                        $modalInstance.close();
                                    }else{
                                        $scope.pop(data.message,"error");
                                    }
                                }
                            });
                        }else{
                            $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
})();